#!/usr/bin/env python
# _*_ coding: utf-8 _*_
"""
@author: caimengzhi
@license: (C) Copyright 2013-2017.
@contact: 610658552@qq.com
@software: pycharm 2017.02
@project: luxffy
@file: management.py
@time: 18-3-2 上午9:11
@desc:
"""

from core import main


class ManagementTool(object):
    len_sys_argv_min = 2
    def __init__(self,sys_argv):
        self.sys_argv = sys_argv
        # print("sys_argv = ",sys_argv)
        self.verify_argv()

    def verify_argv(self):
        if len(self.sys_argv) < self.len_sys_argv_min:
            self.help_msg()

        cmd = self.sys_argv[1]
        if not hasattr(self,cmd):
            print("invalid argument!")
            self.help_msg()

    def help_msg(self):
        msg = '''
        ftp server start mode: 
        python3 server_start.py  start       [ start FTP server ] 
        python3 server_start.py  stop        [ stop FTP　server ]
        python3 server_start.py  restart     [ restart FTP server ]
        python3 server_start.py  createuser  username    [ create a ftp user ]
        '''
        exit(msg)

    def execute(self):
        cmd = self.sys_argv[1]
        getattr(self,cmd)()

    def start(self):
        # print(self.sys_argv)
        server = main.FTPServer(self)
        server.run_forever()

    def stop(self):
        print(self.sys_argv)

    def restart(self):
        print(self.sys_argv)
        self.start()
        self.stop()

    def create_user(self):
        print(self.sys_argv)

